package com.domain.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 解析properties工具类
 */
public class PropertiesParse {
    private static Logger logger = LoggerFactory.getLogger(PropertiesParse.class);
    /**
     * 解析properties
     * @param path 根路径到配置路径
     * @param key key值
     * @return  相对key的value
     */
    public static String parse(String path, String key) {
        try {
            InputStream is = PropertiesParse.class.getResourceAsStream(path);
            Properties ps = new Properties();
            //读取文件
            ps.load(is);
            //查看文件中指定键的值
            return ps.getProperty(key);
        } catch (IOException e) {
            logger.error("IO异常", e);
            return null;
        } catch (Exception e) {
            logger.error("解析文件出现错误", e);
            return null;
        }
    }


    public static String getConfigPropertiesValue(String key) {
        return parse("/config.properties", key);
    }
}
